package data;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class POSItem extends AbstractData {
	private long id = -1;
	//private long displayId;
	private long hotelId;
	private long posId;
	private String title;
	private String productId;
	private POSCategory category;
	private String categoryString;
	private POSKitchen kitchen;
	private String kitchenString;
	private double price;
	private POSUnit unit;
	private String unitString;
	private List<POSTax> tax;
	private String taxString;
	private String description;
	//private boolean isFavourite;
	private boolean status;
	private boolean avaliable;
	private long favourite;
	
	public void setId(long id) {
		this.id = id;
	}
	
	@Override
	public long getId() {
		return id;
	}

	public POSItem()
	{
		status = true;
		avaliable = true;
		//setFavourite(false);
		setFavourite(0);
	}
	
	public POSItem(long id){
		this.id = id;
		this.status = true;
		this.avaliable = true;
		//this.setFavourite(false);
		setFavourite(0);
	}

//	public long getDisplayId() {
//		return displayId;
//	}
//	public void setDisplayId(long displayId) {
//		this.displayId = displayId;
//	}
	
	public long getPOSId() {
		return posId;
	}

	public void setPOSId(long posId) {
		this.posId = posId;
	}

	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public POSCategory getCategory() {
		if (category == null && !(categoryString == null || categoryString.isEmpty())) {
			POSCategory c = new POSCategory(Long.parseLong(categoryString));	
			c.LoadData();
			category = c;
		}
		return category;
	}

	public void setCategory(POSCategory category) {
		this.categoryString = String.format("%d", category.getId());
		this.category = category;
	}

	public String getCategoryString() {
		return categoryString;
	}

	public void setCategoryString(String categoryString) {
		this.categoryString = categoryString;
	}

	public POSKitchen getKitchen() {
		if (kitchen == null && !(kitchenString == null || kitchenString.isEmpty())) {
			POSKitchen c = new POSKitchen(Long.parseLong(kitchenString));	
			c.LoadData();
			kitchen = c;
		}
		return kitchen;
	}

	public void setKitchen(POSKitchen kitchen) {
		this.kitchenString = String.format("%d", kitchen.getId());
		this.kitchen = kitchen;
	}

	public String getKitchenString() {
		return kitchenString;
	}

	public void setKitchenString(String kitchenString) {
		this.kitchenString = kitchenString;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public POSUnit getUnit() {
		if (unit == null && !(unitString == null || unitString.isEmpty())) {
			POSUnit c = new POSUnit(Long.parseLong(unitString));	
			c.LoadData();
			unit = c;
		}
		return unit;
	}

	public void setUnit(POSUnit unit) {
		this.unitString = String.format("%d", unit.getId());
		this.unit = unit;
	}

	public String getUnitString() {
		return unitString;
	}

	public void setUnitString(String unitString) {
		this.unitString = unitString;
	}

	public List<POSTax> getTax() {
		if ((tax == null || tax.size() == 0) && (taxString != null)) {
			String[] idOfList = SplitList(taxString);
			List<POSTax> list = new ArrayList<POSTax>();
			for (String id : idOfList) {
				POSTax a = new POSTax(Long.parseLong(id));
				list.add(a);
			}
			if(list!=null)
			for(POSTax a : list)
				a.LoadData();
			tax = list;
		}
		return tax;
	}

	public void setTax(List<POSTax> tax) {
		this.taxString =CombineList(tax);
		this.tax = tax;
	}

	public String getTaxString() {
		return taxString;
	}

	public void setTaxString(String taxString) {
		this.taxString = taxString;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

//	public boolean isFavourite() {
//		return isFavourite;
//	}
//
//	public void setFavourite(boolean isFavourite) {
//		this.isFavourite = isFavourite;
//	}

	public long getFavourite() {
		return favourite;
	}

	public void setFavourite(long favourite) {
		this.favourite = favourite;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isAvaliable() {
		return avaliable;
	}

	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.POSItem);
		else
			item = new Entity(this.key);

//		item.setProperty("displayId", this.displayId);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("posId", this.posId);
		item.setProperty("title", this.title);
		item.setProperty("productId", this.productId);
		item.setProperty("category", this.categoryString);
		item.setProperty("kitchen", this.kitchenString);
		item.setProperty("price", this.price);
		item.setProperty("unit", this.unitString);
		item.setProperty("tax", this.taxString);
		item.setProperty("description", this.description);
		//item.setProperty("isFavourite", this.isFavourite);
		item.setProperty("favourite", favourite);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);
		
		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
//		this.displayId = (Long) entity.getProperty("displayId");
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.posId = (Long) entity.getProperty("posId");
		this.title = (String) entity.getProperty("title");
		this.productId = (String) entity.getProperty("productId");
		this.categoryString = (String) entity.getProperty("category");
		this.kitchenString = (String) entity.getProperty("kitchen");
		this.price = (Double) entity.getProperty("price");
		this.unitString = (String) entity.getProperty("unit");
		this.taxString = (String) entity.getProperty("tax");
		this.description = (String) entity.getProperty("description");
		//this.isFavourite = (Boolean) entity.getProperty("isFavourite");
		this.favourite = (Long) entity.getProperty("favourite");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");

		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.POSItem, this.id);
			setEntity(entity);
		}
	}
}
